package com.newcapec.erp.ui;

import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.main.LoginManager;
import com.newcapec.erp.utils.XMLFReader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class MainUI {
    // 实例化窗体对象
    private static JFrame frame = new JFrame("主界面");

    //实例化表格对象
    public static JTabbedPane tab;

    //主界面UI
    public static void getMainUI(){
        Container cont = frame.getContentPane();
        cont.setLayout(null);

        //主界面顶部带背景图片Panel
        JPanel topPanel = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                ImageIcon ii = new ImageIcon("images/top.jpg");
                g.drawImage(ii.getImage(), 0, 0, getWidth(), getHeight(), ii.getImageObserver());
            }
        };
        topPanel.setBounds(0, 0, 1200, 52);
        topPanel.setPreferredSize(new Dimension(1200, 52));
        topPanel.setLayout(null);
        //登录信息展示
        JLabel loginLabel = new JLabel(LoginManager.currentLoginEntity.getLoginName()+"[管理员]");
        loginLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        loginLabel.setForeground(Color.WHITE);
        loginLabel.setBounds(950, 12, 100, 30);
        topPanel.add(loginLabel);
        //退出登录按钮
        JButton exitBtn = new JButton("退出登录");
        exitBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        exitBtn.setBounds(1060,20,100,20);
        topPanel.add(exitBtn);

        //顶部菜单栏
        JMenuBar menuBar = MenuBarUI.getMenuBar();
        frame.setJMenuBar(menuBar);

        // 设置Tab标签在左侧显示
        tab = new JTabbedPane(JTabbedPane.LEFT);
        tab.setBorder(null);
        tab.setFont(new Font("微软雅黑",Font.PLAIN,18));
        tab.setBounds(10,55,1150,680);

        for(int i = 0; i < DataInit.manageTypes.size(); i++){
            Vector<Vector<String>> data = XMLFReader.getData(i);
            JPanel panel = TableUI.getTableUI(i,data,1);
            tab.addTab(DataInit.manageTypes.get(i),panel);
        }
        cont.add(topPanel);
        cont.add(tab);
        frame.setSize(1200, 800);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocation(120,50);
        frame.setVisible(true);
    }

    public static void updateMainUI(int type, Vector<Vector<String>> data, int page){
        JPanel panel = TableUI.getTableUI(type,data,page);
        tab.setComponentAt(type,panel);
    }
}
